/**
 * @param {number[]} plants
 * @param {number} capacityA
 * @param {number} capacityB
 * @return {number}
 */
 var minimumRefill = function(plants, capacityA, capacityB) {
  let ans = 0, bagA = capacityA, bagB = capacityB
  const isWater = new Array(plants.length).fill(false)
  const water = (capacity, bag, i) => {
    if(bag >= plants[i]){
      bag -= plants[i]
    }else{
      bag = capacity - plants[i]
      ans++
    }
    isWater[i] = true
    return bag
  }
  for(let i = 0, j = plants.length-1; i<=j; i++, j--){
    if(i === j){
      if(bagA >= bagB){
        bagA = water(capacityA, bagA, i)
      }else{
        bagB = water(capacityB, bagB, j)
      }
    }else{
      bagA = water(capacityA, bagA, i)
      bagB = water(capacityB, bagB, j)
    }
  }
  return ans
};
